/**
 * 简谱打谱软件 - 导出功能模块
 */

// 导出为XML格式
function exportToXML() {
  // 创建项目数据对象
  const projectData = {
    metadata: {
      title: document.getElementById('song-title').value,
      composer: document.getElementById('composer').value,
      lyricist: document.getElementById('lyricist').value,
      keySignature: document.getElementById('key-signature').value,
      timeSignature: document.getElementById('time-signature').value,
      tempo: document.getElementById('tempo').value
    },
    bars: appState.bars,
    lyrics: appState.lyrics
  };
  
  // 转换为XML
  const xml = `<?xml version="1.0" encoding="UTF-8"?>
<sheetMusic>
  <metadata>
    <title>${escapeXml(projectData.metadata.title)}</title>
    <composer>${escapeXml(projectData.metadata.composer)}</composer>
    <lyricist>${escapeXml(projectData.metadata.lyricist)}</lyricist>
    <keySignature>${escapeXml(projectData.metadata.keySignature)}</keySignature>
    <timeSignature>${escapeXml(projectData.metadata.timeSignature)}</timeSignature>
    <tempo>${projectData.metadata.tempo}</tempo>
  </metadata>
  <bars>
    ${projectData.bars.map(bar => `
      <bar>
        ${bar.map(note => `<note>${escapeXml(note)}</note>`).join('')}
      </bar>
    `).join('')}
  </bars>
  <lyrics>
    ${projectData.lyrics.map(line => `<line>${escapeXml(line)}</line>`).join('')}
  </lyrics>
</sheetMusic>`;
  
  // 创建下载链接
  const blob = new Blob([xml], { type: 'text/xml' });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = `${projectData.metadata.title || '简谱'}.xml`;
  document.body.appendChild(a);
  a.click();
  
  // 清理
  setTimeout(() => {
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
  }, 100);
}

// 导出为JSON格式
function exportToJSON() {
  // 创建项目数据对象
  const projectData = {
    metadata: {
      title: document.getElementById('song-title').value,
      composer: document.getElementById('composer').value,
      lyricist: document.getElementById('lyricist').value,
      keySignature: document.getElementById('key-signature').value,
      timeSignature: document.getElementById('time-signature').value,
      tempo: document.getElementById('tempo').value
    },
    bars: appState.bars,
    lyrics: appState.lyrics
  };
  
  // 转换为JSON
  const json = JSON.stringify(projectData, null, 2);
  
  // 创建下载链接
  const blob = new Blob([json], { type: 'application/json' });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = `${projectData.metadata.title || '简谱'}.json`;
  document.body.appendChild(a);
  a.click();
  
  // 清理
  setTimeout(() => {
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
  }, 100);
}

// 导出为文本格式
function exportToText() {
  // 创建项目数据对象
  const projectData = {
    metadata: {
      title: document.getElementById('song-title').value,
      composer: document.getElementById('composer').value,
      lyricist: document.getElementById('lyricist').value,
      keySignature: document.getElementById('key-signature').value,
      timeSignature: document.getElementById('time-signature').value,
      tempo: document.getElementById('tempo').value
    },
    bars: appState.bars,
    lyrics: appState.lyrics
  };
  
  // 转换为文本
  let text = `${projectData.metadata.title || ''}\n`;
  text += `${projectData.metadata.composer || ''}\n`;
  text += `${projectData.metadata.lyricist || ''}\n`;
  text += `${projectData.metadata.keySignature || 'C'}\n`;
  text += `${projectData.metadata.timeSignature || '4/4'}\n`;
  text += `${projectData.metadata.tempo || 120}\n\n`;
  
  // 添加小节
  text += projectData.bars.map(bar => bar.join(' ')).join('\n') + '\n\n';
  
  // 添加歌词
  text += projectData.lyrics.join('\n');
  
  // 创建下载链接
  const blob = new Blob([text], { type: 'text/plain' });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = `${projectData.metadata.title || '简谱'}.txt`;
  document.body.appendChild(a);
  a.click();
  
  // 清理
  setTimeout(() => {
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
  }, 100);
}

// 转义XML特殊字符
function escapeXml(text) {
  return text
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&apos;');
}
    